package cn.rkylin.oms.common.base;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import cn.rkylin.apollo.common.util.BeanUtils;
import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.system.project.service.ProjectManagerService;
import cn.rkylin.oms.system.project.vo.ProjectVO;
import cn.rkylin.oms.system.shop.vo.ShopVO;

/**
 * 抽象适配器工厂
 * @author wangxiaoyi
 *
 */
public abstract class AbstractAdapterFactory {
	/**
	 * 日志工具
	 */
	private static final Log logger = LogFactory.getLog(AbstractAdapterFactory.class);
	
	/**
	 * 根据shopId取得shop对象(支持缓存，缓存没有到库里找)
	 * 
	 * @param shopid
	 * @return 店铺VO
	 */
//	@Cacheable(value = "shop", key = "T(String).valueOf('shop:').concat(#shopid)")
	public static ShopVO getShop(String shopId) {
		IDataBaseFactory dao = BeanUtils.getBean("dataBaseFactory");
		ShopVO shopVO = null;
		try {
			if (StringUtils.isNotEmpty(shopId)) {
				List<ShopVO> resultList = dao.findList("selectByPrimaryKeyShop", shopId);
				if (resultList != null && resultList.size() > 0) {
					shopVO = resultList.get(0);	
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return shopVO;
	}
	
    /**
     * 根据shopId取得项目对象(支持缓存，缓存没有到库里找)
     * 
     * @param shopid
     * @return 项目vo
     * @throws Exception
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static ProjectVO getProject(String shopId) {
        ProjectVO projectVO = null;
        if (StringUtils.isEmpty(shopId)) {
            logger.debug("【shopId为空】");
            return null;
        }
        try {
            ShopVO shop = getShop(shopId);
            if (shop == null) {
                logger.debug("【店铺:" + shopId + "不存在】");
                return null;
            }

            // 缓存里找不到，去db找，并且缓存起来以备下次使用。
            ProjectManagerService projectManagerService = BeanUtils.getBean("projectManagerService");
            if (projectManagerService == null) {
                return null;
            }
            projectVO = projectManagerService.findProjectById(shop.getPrjId());
            if (projectVO == null) {
                return null;
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return projectVO;
    }
}
